﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FacturIT.Service.Repositories.Project;
using FacturIT.Service.Repositories.CompanyProject;
using FacturIT.Service.CompanyProject;

namespace FacturIT.Service.Project
{
    internal class CompanyProjectFactory
    {
        private static CompanyProjectFactory instance = null;
        public static CompanyProjectFactory Instance
        {
            get
            {
                if (instance == null)
                    instance = new CompanyProjectFactory();
                return instance;
            }
        }

        private CompanyProjectService CompanyProjectService;
        public CompanyProjectService GetCompanyProjectService()
        {
            if (CompanyProjectService == null)
                CompanyProjectService = new CompanyProjectService();

            return CompanyProjectService;
        }
    }

    internal class CompanyProjectService
    {
        private IRepository<BLL.Model.CompanyProject.CompanyProject, int> repository;
        private IRepository<BLL.Model.CompanyProject.CompanyProject, int> Repository
        {
            get
            {
                if (repository == null)
                    repository = new CompanyProjectRepository();

                return repository;
            }
        }

        internal CompanyProjectDTO AddCompanyToProject(int projectid, int companyid)
        {
            CompanyProjectDTO CompanyProject = Convert(Repository.Add(new BLL.Model.CompanyProject.CompanyProject()
            {
                ProjectID = projectid,
                CompanyID = companyid
            }));
            return CompanyProject;
        }

        internal CompanyProjectDTO ReadCompanyProjectByProjectAndCompany(int projectid, int companyid)
        {
            return Convert(((CompanyProjectRepository)Repository).ReadCompanyProjectByProjectAndCompany(projectid, companyid));
        }

        internal IEnumerable<CompanyProjectDTO> ReadAllCompaniesFromProject(int projectID)
        {
            return from project in ((CompanyProjectRepository)Repository).ReadAllCompaniesFromProject(projectID) select new CompanyProjectDTO(project);
        }
        #region HelperMethods

        private static CompanyProjectDTO Convert(BLL.Model.CompanyProject.CompanyProject CompanyProject)
        {
            return new CompanyProjectDTO(CompanyProject);
        }
        #endregion   
    
        
    
        
    
        
    }
        
    
    internal class ProjectFactory
    {
        private static ProjectFactory instance = null;
        public static ProjectFactory Instance
        {
            get
            {
                if (instance == null)
                    instance = new ProjectFactory();
                return instance;
            }
        }

        private ProjectService ProjectService;
        public ProjectService GetProjectService()
        {
            if (ProjectService == null)
                ProjectService = new ProjectService();

            return ProjectService;
        }
    }
    internal class ProjectService
    {
        private IRepository<BLL.Model.Project.Project, int> repository;
        private IRepository<BLL.Model.Project.Project, int> Repository
        {
            get
            {
                if (repository == null)
                    repository = new ProjectRepository();

                return repository;
            }
        }

        internal ProjectDTO AddProject(string name, DateTime start, DateTime end, bool active, int paymenttype, int customer)
        {
            ProjectDTO Project = Convert(Repository.Add(new BLL.Model.Project.Project()
            {
                Name = name,
                StartDate = start,
                EndData = end,
                Active = active,
                PaymentType = paymenttype,
                Customer = customer,
            }));
            return Project;
        }

        internal ProjectDTO ReadProject(int id)
        {
            return Convert(Repository.Read(id));
        }

        internal IEnumerable<ProjectDTO> ReadAllProjectsFromCompany(int companyID)
        {
            return from project in ((ProjectRepository)Repository).ReadAllProjectsFromCompany(companyID) select new ProjectDTO(project);
        }

        internal IEnumerable<ProjectDTO> ReadProjectByID(int projectid)
        {
            return from project in ((ProjectRepository)Repository).ReadProjectByID(projectid) select new ProjectDTO(project);
        }

        internal ProjectDTO GetProjectByValues(string name, DateTime start, DateTime? end, bool p, int paymenttype, int customer, int company)
        {
            return Convert(((ProjectRepository)Repository).GetProjectByValues(name, start, end, p, paymenttype, customer, company));
        }

        internal ProjectDTO ReadPaymentTypeFromProject(int projectID)
        {
            return Convert(((ProjectRepository)Repository).ReadPaymentTypeFromProject(projectID));
        }

        internal IEnumerable<ProjectDTO> ReadAllActiveProjectsFromCompany(int companyid)
        {
            return from project in ((ProjectRepository)Repository).ReadAllActiveProjectsFromCompany(companyid) select new ProjectDTO(project);
        }
        #region HelperMethods

        private static ProjectDTO Convert(BLL.Model.Project.Project Project)
        {
            return new ProjectDTO(Project);
        }
        #endregion











        
    }
}